﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TheoriesBraun.BLL;
using System.Windows.Forms;

namespace TheoriesBraun.DAL
{
    class QuestionDAL
    {
        public DataTable getAllQuestion()
        {
            DataTable dt = null;
            try
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM Question", DBConnect.getConnection());
                SqlDataReader reader = cmd.ExecuteReader();
                dt = new DataTable();
                dt.Load(reader);
                reader.Close();
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void addQuestion(QuestionBLL Question)
        {
            try
            {
                string query = "INSERT INTO [Question] VALUES (N'" + Question.Chapter +
                    "', N'" + Question.Question +
                    "', N'" + Question.Answer +
                    "', N'" + Question.QuestionFile +
                    "', N'" + Question.AnswerFile +
                    "', N'" + Question.Images +
                    "', '" + Question.Type +
                    "', '" + Question.Status + "')";
                SqlCommand cmd = new SqlCommand(query, DBConnect.getConnection());
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void editQuestion(QuestionBLL Question)
        {
            try
            {
                string query = "UPDATE [Question] SET Chapter = N'" + Question.Chapter +
                    "', Question = N'" + Question.Question +
                    "', Answer = N'" + Question.Answer +
                    "', QuestionMediaFile = N'" + Question.QuestionFile +
                    "', AnswerMediaFile = N'" + Question.AnswerFile +
                    "', Images = N'" + Question.Images +
                    "', Type = '" + Question.Type +
                    "', Status = '" + Question.Status +
                    "' WHERE QuestionId = '" + Question.QuestionId + "'";
                SqlCommand cmd = new SqlCommand(query, DBConnect.getConnection());
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void delQuestion(QuestionBLL Question)
        {
            try
            {
                string query = "DELETE FROM [Question] WHERE QuestionId ='" + Question.QuestionId + "'";
                SqlCommand cmd = new SqlCommand(query, DBConnect.getConnection());
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
